package it.free.maya.worddistinct;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.apache.log4j.Logger;

/**
 * Write report analysis on a file
 * @author Stefano Falconetti
 */
public class WordsProcessingReportWriter {

	private File saveToFile = null;
	private List<Word> words = null;
	private static Logger logger = Logger.getLogger(WordsProcessingReportWriter.class);
	
	/**
	 * Class builder
	 * @param sortedWords is the list to dump on a report file
	 */
	public WordsProcessingReportWriter(List<Word> sortedWords, File saveTo){
		this.words = sortedWords;
		this.saveToFile = saveTo;
	}
	
	/**
	 * Actually save data report on a file
	 */
	public void write() throws Exception {
		 
		FileWriter fileWriter = new FileWriter(this.saveToFile);
		
		Word tmpWord = null;
	    
	    StringBuffer stringBuffer = new StringBuffer();

	    for(int i=0; i < this.words.size(); i++){
	    	tmpWord = this.words.get(i);
	    	stringBuffer.append((i + 1) + ">> [" + tmpWord.getWord() + "]:" + tmpWord.getOccurencesCounter() + System.getProperty("line.separator"));
	    }
		
	    fileWriter.write(stringBuffer.toString());
	    
	    fileWriter.flush();
	    fileWriter.close();
	    logger.info("Words distinct report saved on file " + this.saveToFile.getAbsolutePath());
	}
}
